package com.ray.activiti.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.activiti.common.annotation.ActField;
import com.ray.activiti.common.utils.AnnotationUtils;
import com.ray.activiti.service.ExtendActBusinessService;
import com.ray.activiti.table.entity.ExtendActBusiness;
import com.ray.activiti.table.mapper.ExtendActBusinessMapper;
import com.ray.common.util.StringUtil;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.util.UUIDUtil;


/**
 * 
 * @Auther ray  qray686898@163.com
 * @Date 2017/12/28
 * @代码参考或来自  https://github.com/huangxianyuan/hxyFrame
 *
 */
@Service
public class ExtendActBusinessServiceImpl implements ExtendActBusinessService {
	@Autowired
	private ExtendActBusinessMapper extendActBusinessMapper;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ExtendActBusiness queryObject(String id){
	    if(org.apache.commons.lang.StringUtils.isEmpty(id)){
	        throw new BussinessException(-1,"节点id不能为空");
        }
		return extendActBusinessMapper.queryObject(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ExtendActBusiness> queryList(Map<String, Object> map){
		return extendActBusinessMapper.queryList(map);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public int queryTotal(Map<String, Object> map){
		return extendActBusinessMapper.queryTotal(map);
	}

    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) 
	public int delete(String id){
		return extendActBusinessMapper.delete(id);
	}
	
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) 
	public void deleteBatch(String[] ids){
		extendActBusinessMapper.deleteBatch(ids);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ExtendActBusiness> queryListByBean(ExtendActBusiness extendActBusinessEntity,LoginUser currentUser) {
        if(!currentUser.isSuperManager()){
        	extendActBusinessEntity.setTenantid(currentUser.getTenantId());
        }
		List<ExtendActBusiness> busList = extendActBusinessMapper.queryListByBean(extendActBusinessEntity);
        return busList;
	}

    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) 
    public int edit(ExtendActBusiness bus,LoginUser currentUser) {
     
    	  bus.setBaid(currentUser.getDeptCode());
          bus.setBapid(currentUser.getOrgCode());
        //根据流程key查询
        ExtendActBusiness businessEntity = extendActBusinessMapper.queryByActKey(bus.getActKey());
        if(businessEntity != null && !StringUtil.equals(bus.getId(), businessEntity.getId())){
            throw new BussinessException(-1,"流程key已经存在,请重复输入");
        }
        if(org.apache.commons.lang.StringUtils.isEmpty(bus.getId())){
            //保存
            bus.setCreateTime(new Date());
            bus.setCreateId(currentUser.getUserId());
            bus.setId(UUIDUtil.getUUID());
            bus.setTenantid(currentUser.getTenantId());
            return extendActBusinessMapper.save(bus);
        }else {
            //更新
            bus.setUpdateId(currentUser.getUserId());
            bus.setUpdateTime(new Date());
            return extendActBusinessMapper.update(bus);
        }
    }

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ExtendActBusiness queryActBusByModelId(String modelId) {
        ExtendActBusiness businessEntity = extendActBusinessMapper.queryActBusByModelId(modelId);
        //业务实体类
        String classurl = businessEntity.getClassurl();
        List<Map<String,Object>> writes=new ArrayList<Map<String,Object>>();//可写
        List<Map<String,Object>> judgs=new ArrayList<Map<String,Object>>();//可设置为条件
        Map<String,Object> temMap = new HashMap<String,Object>();
        temMap.put("value", "isAgree");
        temMap.put("name", "是否通过");
        judgs.add(temMap);
        //writes.add(temMap);
        List<Map<String, Object>> mapList = AnnotationUtils.getActFieldByClazz(classurl);
        for (Map<String,Object> remap:mapList){
            temMap = new HashMap<>();
            ActField actField= (ActField) remap.get("actField");
            String keyName = (String) remap.get("keyName");
            if(actField!=null){
                temMap.put("value", keyName);
                temMap.put("name", actField.name());
                writes.add(temMap);
                if(actField.isJudg()){
                    temMap.put("allow", actField.isJudg());
                    judgs.add(temMap);
                }
            }
        }
        businessEntity.setJudgs(judgs);
        businessEntity.setWrites(writes);
        return businessEntity;
    }

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
    public List<Map<String,Object>> queryCalBackByPid(String parentId) {
        return extendActBusinessMapper.queryCalBackByPid(parentId);
    }

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ExtendActBusiness queryByActKey(String actKey) {
        return extendActBusinessMapper.queryByActKey(actKey);
    }


	
}
